<template>
  <div class="product-card" @click="goToItemDetail">
    <img
      :src="product.image"
      :alt="product.name"
      class="product-image"
      @load="$emit('imageLoad')" />
    <div class="product-info">
      <div class="product-info-row">
        <h3>{{ product.name }}</h3>
        <p class="price">¥{{ product.price }}</p>
      </div>
      <div class="product-info-row">
        <p>销量: {{ product.sales }}</p>
        <p>评论: {{ product.comments }}</p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { defineProps } from 'vue'
  import { useRouter } from 'vue-router'

  const router = useRouter()

  const props = defineProps<{
    product: {
      id: number
      image: string
      name: string
      price: number
      sales: number
      comments: number
      isAd: boolean
    }
  }>()

  defineEmits(['imageLoad'])

  const goToItemDetail = () => {
  router.push({
    name: 'item',
    params: { id: props.product.id },
    query: {
      from: 'product_list'
    }
  })
}
</script>

<style scoped>
  .product-card {
    display: flex;
    flex-direction: column;
    width: 260px;
    height: 360px;
    background-color: #fff;
    border: 1px solid #ddd;
    border-radius: 8px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    position: relative;
  }

  .product-image {
    width: 100%;
    height: 85%;
    object-fit: cover;
    transition: transform 0.3s ease;
    overflow: hidden;
    transform-origin: center bottom;
  }

  .product-card:hover .product-image {
    transform: scale(1.2);
  }

  .product-info {
    height: 15%;
    padding: 10px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
  }

  .product-info-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .product-info h3 {
    font-size: 16px;
    font-weight: bold;
    margin: 0;
  }

  .product-info p {
    margin: 0;
    color: #666;
    font-size: 14px;
  }

  .product-info p.price {
    color: red;
    font-weight: bold;
  }
</style>
